package com.yu.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Comparator;

public class ComparatorDTO<T> implements Comparator<T> {

	private String attr;
	private String sortKey;

	public ComparatorDTO(String attr) {
		this.attr = attr;
		sortKey = "desc";
	}

	public ComparatorDTO(String attr, String sortKey) {
		this.attr = attr;
		this.sortKey = sortKey;
	}

	@Override
	public int compare(T o1, T o2) {
		int result = 0;
		try {
			// 數字 类型比较
			if ("desc".equals(sortKey)) {

				result = getCompareForNumber(o2).compareTo(
						getCompareForNumber(o1));

			} else {
				result = getCompareForNumber(o1).compareTo(
						getCompareForNumber(o2));
			}
			if (result == 0) {
				// 字符串比較
				if ("desc".equals(sortKey)) {
					result = getCompareForString(o2).compareTo(
							getCompareForString(o1));
				} else {
					result = getCompareForString(o1).compareTo(
							getCompareForString(o2));
				}
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * String 类型比较
	 * 
	 * @param order
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	private String getCompareForString(T t) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException,
			SecurityException, NoSuchMethodException {
		Class<?> cls = t.getClass();
		Method mes = cls.getDeclaredMethod("get" + attr);
		Object value = mes.invoke(t);
		if (value instanceof String) {
			return (String) value;
		}
		return "";
	}

	/**
	 * 数字类型BigDecimal 比较
	 * 
	 * @param order
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	private BigDecimal getCompareForBigDecimal(T t)
			throws IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, SecurityException, NoSuchMethodException {
		Class<?> cls = t.getClass();
		Method mes = cls.getDeclaredMethod("get" + attr);
		Object value = mes.invoke(t);
		if (value instanceof BigDecimal) {
			return (BigDecimal) value;
		}
		return new BigDecimal("0.00");
	}

	/**
	 * int 类型比较
	 * 
	 * @param order
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	private int getCompareForInteger(T t) throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException,
			SecurityException, NoSuchMethodException {
		Class<?> cls = t.getClass();
		Method mes = cls.getDeclaredMethod("get" + attr);
		Object value = mes.invoke(t);
		if (value instanceof Integer) {
			return (Integer) value;
		}
		return 0;
	}

	/**
	 * int 类型比较
	 * 
	 * @param order
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws SecurityException
	 * @throws NoSuchMethodException
	 */
	private BigDecimal getCompareForNumber(T t)
			throws IllegalArgumentException, IllegalAccessException,
			InvocationTargetException, SecurityException, NoSuchMethodException {
		Class<?> cls = t.getClass();
		Method mes = cls.getDeclaredMethod("get" + attr);
		Object value = mes.invoke(t);
		if (value instanceof Number) {
			return new BigDecimal(value.toString());
		}
		return new BigDecimal(0);
	}

}
